package com.atguigu.day05;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

public class Flink02_CountWindow {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        SingleOutputStreamOperator<WaterSensor> map = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //3.将相同id的数据聚合到一块
        KeyedStream<WaterSensor, String> keyedStream = map.keyBy(r -> r.getId());

       //TODO 4.开启一个窗口大小为5 的计数滚动窗口
//        WindowedStream<WaterSensor, String, GlobalWindow> countWindow = keyedStream.countWindow(5);
        //TODO 开启一个窗口大小为5,滑动步长为2 的计数滑动窗口
        WindowedStream<WaterSensor, String, GlobalWindow> countWindow = keyedStream.countWindow(5,2);

        countWindow.sum("vc").print();

        env.execute();



    }
}
